package pers.wk.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import pers.wk.consumer.WeChatConsumer;

/**
 * JavaConfig配置MQ信息
 */
@Configuration
public class RabbitMQConfig {

    private static final String WECHAT_QUEUE_NAME = "wechat.queue";
    private static final String WECHAT_EXCHANGE_NAME = "wechat.exchange";
    private static final String WECHAT_ROUTING_KEY = "wechat.key";

    /**
     * declare queue
     *
     * @return
     */
    @Bean(value = WECHAT_QUEUE_NAME)
    public Queue queue() {
        //用withArguments() 方法 绑定参数，比如死信队列
        return QueueBuilder.durable(WECHAT_QUEUE_NAME).build();
    }

    /**
     * declare exchange
     *
     * @return
     */
    @Bean(value = WECHAT_EXCHANGE_NAME)
    public DirectExchange exchange() {
        return (DirectExchange) ExchangeBuilder.directExchange(WECHAT_EXCHANGE_NAME).durable(true).build();
    }

    /**
     * 声明队列交换机绑定关系
     *
     * @param queue
     * @param exchange
     * @return
     */
    @Bean
    public Binding bingQueueExchange(@Qualifier(value = WECHAT_QUEUE_NAME) Queue queue
            , @Qualifier(value = WECHAT_EXCHANGE_NAME) DirectExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(WECHAT_ROUTING_KEY);
    }

    /**
     * declare connectionFactorys
     *
     * @param host
     * @param port
     * @param username
     * @param password
     * @param vhost
     * @return
     */
    @Bean("connectionFactory")
    public ConnectionFactory connectionFactory(
            @Value("${spring.rabbitmq.addresses}") String addresses,
            @Value("${spring.rabbitmq.host}") String host,
            @Value("${spring.rabbitmq.port}") int port,
            @Value("${spring.rabbitmq.username}") String username,
            @Value("${spring.rabbitmq.password}") String password,
            @Value("${spring.rabbitmq.virtual-host}") String vhost) {
        //负载均衡集群
//        CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
//        connectionFactory.setAddresses(addresses);
        //单节点
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory(host);
        connectionFactory.setPort(port);

        connectionFactory.setUsername(username);
        connectionFactory.setPassword(password);
        connectionFactory.setVirtualHost(vhost);
        connectionFactory.setPublisherConfirms(true);
        connectionFactory.setPublisherReturns(true);
        return connectionFactory;
    }

    /**
     * declare containers
     *
     * @param connectionFactory
     * @param queue
     * @param weChatConsumer
     * @return
     */
    @Bean
    public SimpleMessageListenerContainer container(
            @Qualifier("connectionFactory") ConnectionFactory connectionFactory,
            @Qualifier(WECHAT_QUEUE_NAME) Queue queue,
            @Qualifier("weChatConsumer") WeChatConsumer weChatConsumer) {
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        container.setQueues(queue);
        container.setMaxConcurrentConsumers(15);
        container.setConcurrentConsumers(15);
        // 手动确认
        container.setAcknowledgeMode(AcknowledgeMode.MANUAL);
        container.setMessageListener(weChatConsumer);
        return container;
    }
}
